function UserListCtrl($scope, $http) {
	var role;
	if ($scope.role == 0) {
		role = true;
	} else {
		role = false;
	}

	jQuery(function($) {
		var grid_selector = "#grid-table";
		var pager_selector = "#grid-pager";

		jQuery(grid_selector)
				.jqGrid(
						{
							// direction: "rtl",
							// data : grid_data,
							// datatype : "local",
							url : "/YYOA/user/list",
							contentType : 'application/json',
							mtype : "post",
							datatype : 'json',
							height : "100%",
							colNames : [ ' ', 'ID', '用户名', '真实姓名', '性别', '邮箱',
									'电话', '手机', '备注' ],
							colModel : [
									{
										name : 'myac',
										index : '',
										width : 80,
										fixed : true,
										hidden : !role,
										sortable : false,
										resize : false,
										formatter : 'actions',
										search : false,
										formatoptions : {
											keys : false,
											delOptions : {
												recreateForm : true,
												beforeShowForm : beforeDeleteCallback,
												ajaxDelOptions : {
													url : "/YYOA/user/modify",
													success : function(result) {
														if (result.error != null) {
															alert(result.error);
														} else {
															alert("删除成功！");
														}
													},
													error : function(result) {
														var responseText = result.responseText;
														if (responseText != '') {
															alert(JSON
																	.parse(responseText).error);
														}
													}
												}
											},
											editformbutton : true,
											editOptions : {
												closeAfterEdit : true,
												recreateForm : true,
												beforeShowForm : beforeEditCallback
											}
										}
									},
									{
										name : 'id',
										index : 'id',
										width : 15,
										sorttype : "int",
										editable : false,
										search : true,
										// stype : "select",
										searchoptions : {
											sopt : [ "eq", "ne" ]
										}
									},
									{
										name : 'userName',
										index : 'userName',
										width : 50,
										editable : false,
										editrules : {
											required : true,
											custom : true,
											custom_func : checkUserNameUnique
										},
										editoptions : {
											size : "20",
											maxlength : "100"
										},
										searchoptions : {
											sopt : [ 'eq', 'ne', 'bw', 'bn',
													'ew', 'en', 'cn', 'nc' ]
										}
									},
									{
										name : 'name',
										index : 'name',
										width : 50,
										editable : true,
										editoptions : {
											size : "20",
											maxlength : "100"
										},
										search : true,
										searchoptions : {
											sopt : [ 'eq', 'ne', 'bw', 'bn',
													'ew', 'en', 'cn', 'nc' ]
										}
									},
									{
										name : 'sex',
										index : 'sex',
										width : 20,
										editable : true,
										required : true,
										edittype : "select",
										editoptions : {
											value : "男:男;女:女"
										},
										search : true,
										searchoptions : {
											sopt : [ "eq", "ne" ],
										// stype : "select",
										// dataUrl :
										// "../../../pc/page/static/sexList.html"
										}
									},
									{
										name : 'email',
										index : 'email',
										width : 70,
										editable : true,
										editrules : {
											required : true,
											email : true,
											custom : true,
											custom_func : checkEmailUnique
										},
										editoptions : {
											size : "20",
											maxlength : "100"
										},
										search : true,
										searchoptions : {
											sopt : [ 'eq', 'ne', 'bw', 'bn',
													'ew', 'en', 'cn', 'nc' ]
										}
									},
									{
										name : 'phone',
										index : 'phone',
										width : 50,
										editable : true,
										editoptions : {
											size : "20",
											maxlength : "80"
										},
										search : true,
										searchoptions : {
											sopt : [ 'eq', 'ne', 'bw', 'bn',
													'ew', 'en', 'cn', 'nc' ]
										}
									},
									{
										name : 'mobilePhone',
										index : 'mobilePhone',
										width : 50,
										editable : true,
										editrules : {
											required : true,
											number : true,
											custom : true,
											custom_func : checkMobilePhoneUnique
										},
										editoptions : {
											size : "20",
											maxlength : "80"
										},
										search : true,
										searchoptions : {
											sopt : [ "eq", "ne", "bw", "bn",
													"ew", "en", "cn", "nc" ]
										}
									}, {
										name : 'describe',
										index : 'describe',
										width : 150,
										sortable : false,
										editable : true,
										edittype : "textarea",
										editoptions : {
											rows : "2",
											cols : "10"
										},
										search : true,
										searchoptions : {
											sopt : [ 'eq', 'ne', 'cn', 'nc' ]
										}
									} ],
							jsonReader : {
								root : "result",
								page : "pageNo",
								total : "totalPages",
								records : "totalCount",
								repeatitems : false,
								cell : "cell"
							},
							viewrecords : true,
							rowNum : 2,
							rowList : [ 2, 4, 10 ],
							pager : pager_selector,
							altRows : true,
							// toppager : true,

							multiselect : true,
							// multikey : "ctrlKey",
							multiboxonly : true,
							loadComplete : function() {
								var table = this;
								setTimeout(function() {
									styleCheckbox(table);
									updateActionIcons(table);
									updatePagerIcons(table);
									enableTooltips(table);
								}, 0);

								var errorMessage = $(this)
										.getGridParam('error');
								if (errorMessage != null) {
									alert(errorMessage);
								}
							},
							loadError : function(xhr, status, error) {
								alert(status + " loading data of "
										+ $(this).attr("id") + " : " + error);
							},
							editurl : "/YYOA/user/modify",// nothing is saved
							caption : "用户列表",
							autowidth : true
						});

		// enable search/filter toolbar
		// jQuery(grid_selector).jqGrid('filterToolbar', {
		// defaultSearch : true,
		// stringResult : true,
		//			
		// })

		// switch element when editing inline
		function aceSwitch(cellvalue, options, cell) {
			setTimeout(function() {
				$(cell).find('input[type=checkbox]').wrap(
						'<label class="inline" />').addClass(
						'ace ace-switch ace-switch-5').after(
						'<span class="lbl"></span>');
			}, 0);
		}
		// enable datepicker
		function pickDate(cellvalue, options, cell) {
			setTimeout(function() {
				$(cell).find('input[type=text]').datetimepicker({
					language : 'zh-CN',
					format : 'yyyy-mm-dd',
					weekStart : 1,
					todayBtn : 1,
					autoclose : 1,
					todayHighlight : 1,
					startView : 2,
					forceParse : 0,
					showMeridian : 1,
					minView : 3
				});
			}, 0);
		}

		// navButtons
		jQuery(grid_selector).jqGrid(
				'navGrid',
				pager_selector,
				{ // navbar options
					edit : role,
					editicon : 'icon-pencil blue',
					add : role,
					addicon : 'icon-plus-sign purple',
					del : role,
					delicon : 'icon-trash red',
					search : true,
					searchicon : 'icon-search orange',
					refresh : true,
					refreshicon : 'icon-refresh green',
					view : true,
					viewicon : 'icon-zoom-in grey',
				},
				{
					// edit record form
					// closeAfterEdit: true,
					recreateForm : true,
					closeAfterEdit : true,
					beforeShowForm : function(e) {
						var form = $(e[0]);
						form.closest('.ui-jqdialog').find(
								'.ui-jqdialog-titlebar').wrapInner(
								'<div class="widget-header" />')
						style_edit_form(form);
					}
				},
				{
					// new record form
					url : "/YYOA/user/modify",
					mtype : "POST",
					closeAfterAdd : true,
					recreateForm : true,
					viewPagerButtons : false,
					reloadAfterSubmit : true,
					beforeInitData : function(e) {
						jQuery(grid_selector).setColProp('userName', {
							editable : true
						});
					},
					beforeShowForm : function(e) {
						var form = $(e[0]);
						form.closest('.ui-jqdialog').find(
								'.ui-jqdialog-titlebar').wrapInner(
								'<div class="widget-header" />')
						style_edit_form(form);
					},
					afterShowForm : function(e) {
						jQuery(grid_selector).setColProp('userName', {
							editable : false
						});
					}
				},
				{
					// delete record form
					recreateForm : true,
					beforeShowForm : function(e) {
						var form = $(e[0]);
						if (form.data('styled'))
							return false;

						form.closest('.ui-jqdialog').find(
								'.ui-jqdialog-titlebar').wrapInner(
								'<div class="widget-header" />')
						style_delete_form(form);

						form.data('styled', true);
					},
					ajaxDelOptions : {
						url : "/YYOA/user/modify",
						success : function(result) {
							if (result.error != null) {
								alert(result.error);
							} else {
								alert("删除成功！");
							}
						},
						error : function(result) {
							var responseText = result.responseText;
							if (responseText != '') {
								alert(JSON.parse(responseText).error);
							}
						}

					}
				},
				{
					// search form
					multipleSearch : true,
					multipleGroup : false,
					showQuery : false,
					// sopt : [ 'eq', 'ne', 'lt', 'le', 'gt', 'ge', 'bw', 'bn',
					// 'in', 'ni', 'ew', 'en', 'cn', 'nc' ],
					recreateForm : true,
					afterShowSearch : function(e) {
						var form = $(e[0]);
						form.closest('.ui-jqdialog').find('.ui-jqdialog-title')
								.wrap('<div class="widget-header" />')
						style_search_form(form);
					},
					afterRedraw : function() {
						style_search_filters($(this));
					},
					closeAfterSearch : true,
				},
				{
					// view record form
					recreateForm : true,
					beforeShowForm : function(e) {
						var form = $(e[0]);
						form.closest('.ui-jqdialog').find('.ui-jqdialog-title')
								.wrap('<div class="widget-header" />')
					}
				})

		function checkUserNameUnique(value, colname) {
			var selRowId = $(this).jqGrid('getGridParam', 'selrow');
			var returnInfo = [ true, "" ];
			ajaxJSonPost("/YYOA/user/getByUserName", false, {
				"userName" : value
			}, function(result) {
				if (!checkUserUnique(result, selRowId)) {
					returnInfo = [ false, "此用户名已存在。" ];
				}
			}, null);
			return returnInfo;
		}
		function checkEmailUnique(value, colname) {
			var selRowId = $(this).jqGrid('getGridParam', 'selrow');
			var returnInfo = [ true, "" ];
			ajaxJSonPost("/YYOA/user/getByEmail", false, {
				"email" : value
			}, function(result) {
				if (!checkUserUnique(result, selRowId)) {
					returnInfo = [ false, "此邮箱已存在。" ];
				}
			}, null);
			return returnInfo;
		}

		function checkMobilePhoneUnique(value, colname) {
			var selRowId = $(this).jqGrid('getGridParam', 'selrow');
			var returnInfo = [ true, "" ];
			ajaxJSonPost("/YYOA/user/getByMobilePhone", false, {
				"mobilePhone" : value
			}, function(result) {
				if (!checkUserUnique(result, selRowId)) {
					returnInfo = [ false, "此手机号已存在。" ];
				}
			}, null);
			return returnInfo;
		}

		// contains select row
		function checkUserUnique(result, selRowId) {
			if (result != null && result.id != selRowId) {
				return false;
			}
			return true;
		}

		function style_edit_form(form) {
			// enable datepicker on "sdate" field and switches for "stock" field
			form.find('input[name=sdate]').datetimepicker({
				language : 'zh-CN',
				format : 'yyyy-mm-dd',
				weekStart : 1,
				todayBtn : 1,
				autoclose : 1,
				todayHighlight : 1,
				startView : 2,
				forceParse : 0,
				showMeridian : 1,
				minView : 3
			}).end().find('input[name=stock]').addClass(
					'ace ace-switch ace-switch-5').wrap(
					'<label class="inline" />').after(
					'<span class="lbl"></span>');

			// update buttons classes
			var buttons = form.next().find('.EditButton .fm-button');
			buttons.addClass('btn btn-sm').find('[class*="-icon"]').remove();// ui-icon,
			// s-icon
			buttons.eq(0).addClass('btn-primary').prepend(
					'<i class="icon-ok"></i>');
			buttons.eq(1).prepend('<i class="icon-remove"></i>')

			buttons = form.next().find('.navButton a');
			buttons.find('.ui-icon').remove();
			buttons.eq(0).append('<i class="icon-chevron-left"></i>');
			buttons.eq(1).append('<i class="icon-chevron-right"></i>');
		}

		function style_delete_form(form) {
			var buttons = form.next().find('.EditButton .fm-button');
			buttons.addClass('btn btn-sm').find('[class*="-icon"]').remove();// ui-icon,
			// s-icon
			buttons.eq(0).addClass('btn-danger').prepend(
					'<i class="icon-trash"></i>');
			buttons.eq(1).prepend('<i class="icon-remove"></i>')
		}

		function style_search_filters(form) {
			form.find('.delete-rule').val('X');
			form.find('.add-rule').addClass('btn btn-xs btn-primary');
			form.find('.add-group').addClass('btn btn-xs btn-success');
			form.find('.delete-group').addClass('btn btn-xs btn-danger');
		}
		function style_search_form(form) {
			var dialog = form.closest('.ui-jqdialog');
			var buttons = dialog.find('.EditTable')
			buttons.find('.EditButton a[id*="_reset"]').addClass(
					'btn btn-sm btn-info').find('.ui-icon').attr('class',
					'icon-retweet');
			buttons.find('.EditButton a[id*="_query"]').addClass(
					'btn btn-sm btn-inverse').find('.ui-icon').attr('class',
					'icon-comment-alt');
			buttons.find('.EditButton a[id*="_search"]').addClass(
					'btn btn-sm btn-purple').find('.ui-icon').attr('class',
					'icon-search');
		}

		function afterDeleteSubmitCallback(e) {
			alert("afterSubmit");
		}

		function beforeDeleteCallback(e) {
			var form = $(e[0]);
			if (form.data('styled'))
				return false;

			form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar')
					.wrapInner('<div class="widget-header" />')
			style_delete_form(form);

			form.data('styled', true);
		}

		function beforeEditCallback(e) {
			var form = $(e[0]);
			form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar')
					.wrapInner('<div class="widget-header" />')
			style_edit_form(form);
		}

		// it causes some flicker when reloading or navigating grid
		// it may be possible to have some custom formatter to do this as the
		// grid is being created to prevent this
		// or go back to default browser checkbox styles for the grid
		function styleCheckbox(table) {
			/**
			 * $(table).find('input:checkbox').addClass('ace') .wrap('<label
			 * />') .after('<span class="lbl align-top" />')
			 * 
			 * 
			 * $('.ui-jqgrid-labels th[id*="_cb"]:first-child')
			 * .find('input.cbox[type=checkbox]').addClass('ace') .wrap('<label
			 * />').after('<span class="lbl align-top" />');
			 */
		}

		// unlike navButtons icons, action icons in rows seem to be hard-coded
		// you can change them like this in here if you want
		function updateActionIcons(table) {
			/**
			 * var replacement = { 'ui-icon-pencil' : 'icon-pencil blue',
			 * 'ui-icon-trash' : 'icon-trash red', 'ui-icon-disk' : 'icon-ok
			 * green', 'ui-icon-cancel' : 'icon-remove red' };
			 * $(table).find('.ui-pg-div span.ui-icon').each(function(){ var
			 * icon = $(this); var $class =
			 * $.trim(icon.attr('class').replace('ui-icon', '')); if($class in
			 * replacement) icon.attr('class', 'ui-icon '+replacement[$class]); })
			 */
		}

		// replace icons with FontAwesome icons like above
		function updatePagerIcons(table) {
			var replacement = {
				'ui-icon-seek-first' : 'icon-double-angle-left bigger-140',
				'ui-icon-seek-prev' : 'icon-angle-left bigger-140',
				'ui-icon-seek-next' : 'icon-angle-right bigger-140',
				'ui-icon-seek-end' : 'icon-double-angle-right bigger-140'
			};
			$(
					'.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon')
					.each(
							function() {
								var icon = $(this);
								var $class = $.trim(icon.attr('class').replace(
										'ui-icon', ''));

								if ($class in replacement)
									icon.attr('class', 'ui-icon '
											+ replacement[$class]);
							})
		}

		function enableTooltips(table) {
			$('.navtable .ui-pg-button').tooltip({
				container : 'body'
			});
			$(table).find('.ui-pg-div').tooltip({
				container : 'body'
			});
		}

		// var selr = jQuery(grid_selector).jqGrid('getGridParam','selrow');

	});
}